package com.guli.servicebase.exceptionHandler;


import com.guli.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName GlobalExceptionHandler
 * @Description TODO
 * @Author YinLiangTao Email:1517535831@qq.com
 * @Date 2022/3/29 21:49
 * @Version 1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        System.out.println("全局错误");
        return R.error().message("执行了全局异常处理方法");
    }

    //特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("执行了ArithmeticException异常处理方法");
    }

    //自定义异常处理
    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public R error(GuliException e){
       log.error(e.getMessage());
       e.printStackTrace();
        System.out.println("guliexception 错误");
        return R.error().code(e.getCode()).message(e.getMsg());
    }
}
